<?php
function tuling($msg)
{
    $input = [
        "reqType" => 0,
        "perception" => [
            "inputText" => [
                "text" => $msg
            ]
        ],
        "userInfo" => [
            "apiKey" => "94bcd348d35d4025a456dde81fd4826b",
            "userId" => "1"
        ]
    ];
    $url = 'http://openapi.tuling123.com/openapi/api/v2';
    $data  = json_encode($input);
    $headerArray = array("Content-type:application/json;charset='utf-8'", "Accept:application/json");
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return json_decode($output, true)['results'][0]['values']['text'];
}

function getFreeChat($keywords)
{
    $api = "http://api.qingyunke.com/api.php?key=free&appid=0&msg={$keywords}";
    $json = json_decode(file_get_contents($api), true);
    return $json['result'] == 0 ? $json['content'] : '机器人故障, 请稍后再试!';
}
